import 'package:flutter/material.dart';
import 'package:gif/gif.dart';
import '../../models/home_init_model.dart';
import '../../models/home_info_model.dart';
import 'product_search_view.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import '../../manger/cached_image_widget.dart';

typedef SearchActionCallback = void Function();
typedef SearchKeyCallback = void Function(String key);
typedef PushContactCallback = void Function();
typedef AdTapCallback = void Function(int id);

class HomeTopView extends StatefulWidget {
  final List<HotSearchListModel> searchList;
  final List<AdListModel> homeAdInfo;
  final SearchActionCallback onSearchAction;
  final SearchKeyCallback onSearchKey;
  final PushContactCallback onContact;
  final AdTapCallback? onAdTap;

  const HomeTopView({
    super.key,
    required this.searchList,
    required this.onSearchAction,
    required this.onSearchKey,
    required this.onContact,
    required this.homeAdInfo,
    this.onAdTap,
  });

  @override
  State<HomeTopView> createState() => _HomeTopViewState();
}

class _HomeTopViewState extends State<HomeTopView> with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;

    // 假设广告图宽高比为 343:130
    final double bannerAspectRatio = 343 / 130;
    double bannerWidth = screenWidth - 30;
    double bannerHeight = bannerWidth / bannerAspectRatio;
    double homeTopViewHeight = MediaQuery.of(context).padding.top + 135 + bannerHeight;

    return SizedBox(
      height: homeTopViewHeight,
      child: Stack(
        children: [
          // 背景图
          Image.asset(
            'assets/images/homeTopBg.png',
            width: screenWidth,
            height: homeTopViewHeight,
            fit: BoxFit.cover,
          ),

          // 顶部 logo
          Positioned(
            top: MediaQuery.of(context).padding.top,
            left: screenWidth / 2 - 70,
            child: Image.asset(
              'assets/images/verIcon.png',
              width: 140,
              height: 44,
            ),
          ),

          // 客服 GIF
          Positioned(
            top: MediaQuery.of(context).padding.top,
            right: 14,
            child: GestureDetector(
              onTap: widget.onContact,
              child: Gif(
                fps: 10,
                autostart: Autostart.loop,
                image: AssetImage('assets/images/kefu_logo2.gif'),
                width: 44,
                height: 44,
                fit: BoxFit.cover,
              ),
            ),
          ),

          // 搜索框
          Positioned(
            top: MediaQuery.of(context).padding.top + 44 + 14,
            left: 15,
            right: 15,
            child: ProductSearchView(
              onTap: widget.onSearchAction,
            ),
          ),

          // 搜索热词按钮
          Positioned(
            top: MediaQuery.of(context).padding.top + 44 + 14 + 34 + 8,
            left: 20,
            right: 20,
            height: 28,
            child: Row(
              children: widget.searchList.take(3).map((model) {
                return Padding(
                  padding: const EdgeInsets.only(right: 10),
                  child: ElevatedButton(
                    onPressed: () => widget.onSearchKey(model.name),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.white,
                      shape: const StadiumBorder(),
                      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
                      elevation: 0,
                    ),
                    child: Text(
                      model.name,
                      style: const TextStyle(fontSize: 11, color: Color(0xFF505050)),
                    ),
                  ),
                );
              }).toList(),
            ),
          ),

          // 广告轮播图（自适应高度）
          Positioned(
            top: MediaQuery.of(context).padding.top + 44 + 14 + 34 + 48,
            left: 15,
            right: 15,
            child: AspectRatio(
              aspectRatio: bannerAspectRatio,
              child: Swiper(
                itemCount: widget.homeAdInfo.length,
                itemBuilder: (context, index) {
                  final imageUrl = widget.homeAdInfo[index].cover;
                  return GestureDetector(
                    onTap: () {
                      final spuId = widget.homeAdInfo[index].spuId;
                      if (spuId != '0' && spuId.isNotEmpty) {
                        widget.onAdTap?.call(int.parse(spuId));
                      }
                    },
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(12),
                      child: CachedImage(
                        imageUrl: imageUrl,
                        fit: BoxFit.cover,
                        placeholderPath: 'assets/images/placeholderImage2.png',
                      ),
                    ),
                  );
                },
                autoplay: true,
                autoplayDelay: 3000,
                loop: true,
                pagination: null,
                control: null,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
